#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qqtcore.h"

MainWindow::MainWindow ( QWidget* parent ) :
    QMainWindow ( parent ),
    ui ( new Ui::MainWindow )
{
    ui->setupUi ( this );

    setMinimumSize ( 600, 220 );

    QList<QByteArray> qbaList = QTextCodec::availableCodecs();
    QListIterator<QByteArray> itor ( qbaList );
    while ( itor.hasNext() )
    {
        QByteArray codecName = itor.next();
        ui->comboBox->addItem ( codecName );
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::calculate ( QString path )
{
    QDir d ( path );

    //根目录文件夹是否存在。
    if ( !QDir ( ui->lineEdit_2->text() ).exists() )
        QDir().mkdir ( ui->lineEdit_2->text() );

    foreach ( QFileInfo mfi, d.entryInfoList() )
    {
        if ( mfi.isFile() )
        {
            //获取源文件名
            QString srcFile = mfi.absoluteFilePath();
#ifdef Q_OS_WIN
            srcFile.replace ( "/", "\\" );
#endif

            //获取目标文件名
            QString relativePath = QDir ( ui->lineEdit->text() ).relativeFilePath ( mfi.absolutePath() );
            QString targetPath = ui->lineEdit_2->text() + "/" + relativePath;
            QString targetName = targetPath + "/" + mfi.fileName();

#ifdef Q_OS_WIN
            targetPath.replace ( "/", "\\" );
            targetName.replace ( "/", "\\" );
#endif


            QDir().mkdir ( targetPath );

            //获取源编码。
            QString codecName = getCodecNameForFile ( srcFile );
            if ( codecName.isEmpty() )
            {
                ui->textBrowser->append ( QString ( "get codec fail: " + srcFile ) );
                continue;
            }

            pline() << srcFile << codecName;

            //获取目标编码。
            QString targetCodecName = ui->comboBox->currentText();


            //转换
            QFile file ( srcFile );
            file.open ( QFile::ReadOnly );
            if ( !file.isOpen() )
            {
                ui->textBrowser->append ( QString ( "open source fail: " + srcFile ) );
                continue;
            }
            QByteArray srouceContent = file.readAll();
            file.close();

            QString strUnicode = QTextCodec::codecForName ( codecName.toLocal8Bit() )->toUnicode ( srouceContent );
            QByteArray targetContent = QTextCodec::codecForName ( targetCodecName.toLocal8Bit() )->fromUnicode ( strUnicode );

            pline() << targetName << targetCodecName;



            QFile file2 ( targetName );
            file2.open ( QFile::Truncate | QFile::WriteOnly );
            if ( !file2.isOpen() )
            {
                ui->textBrowser->append ( QString ( "open target fail: " + targetName ) );
                continue;
            }
            file2.write ( targetContent );
            file2.close();

            ui->textBrowser->append ( QString ( "success: " + srcFile ) );
        }
        else
        {
            if ( mfi.fileName() == "." || mfi.fileName() == ".." )
                continue;

            QString relativePath = QDir ( ui->lineEdit->text() ).relativeFilePath ( mfi.absolutePath() );
            QString targetPath = ui->lineEdit_2->text() + "/" + relativePath;
#ifdef Q_OS_WIN
            targetPath.replace ( "/", "\\" );
#endif

            QDir().mkdir ( targetPath );
            pline() << "create dir " << targetPath;

            calculate ( mfi.absoluteFilePath() );
        }
    }
}

QString MainWindow::getCodecNameForFile ( QString fileName )
{
    QFile file ( fileName );
    file.open ( QFile::ReadOnly );
    if ( !file.isOpen() )
        return "";
    QByteArray fileBytes = file.readAll();
    file.close();

    QList<QByteArray> qbaList = QTextCodec::availableCodecs();
    QListIterator<QByteArray> itor ( qbaList );
    while ( itor.hasNext() )
    {
        QByteArray codecName = itor.next();
        //pline() << fileName << codecName;
        QTextCodec::ConverterState state;
        QString text = QTextCodec::codecForName ( codecName )->toUnicode ( fileBytes.constData(), fileBytes.size(), &state );
        if ( state.invalidChars > 0 )
            continue;
        return codecName;
    }
}


void MainWindow::on_pushButton_clicked()
{
    if ( ui->lineEdit->text().isEmpty() )
        return;

    if ( ui->lineEdit_2->text().isEmpty() )
        return;

    ui->textBrowser->clear();


    ui->statusBar->showMessage ( "processing...." );

    calculate ( ui->lineEdit->text() );

    ui->statusBar->showMessage ( "Successed." );

}

void MainWindow::on_lineEdit_textChanged ( const QString& arg1 )
{
#ifdef Q_OS_WIN
    //ui->lineEdit_2->setText ( QString ( arg1 + "..\\targetDirectory" ) );
#else
    //ui->lineEdit_2->setText ( QString ( arg1 + "../targetDirectory" ) );
#endif
}
